<?php

namespace backend\modules\Proprietor\controllers;

use common\helpers\Tool;
use common\models\c2\entity\FeUser;
use common\models\c2\entity\UserDevice;
use cza\base\models\statics\EntityModelStatus;
use cza\base\models\statics\ResponseDatum;
use Yii;
use common\models\c2\entity\Proprietor;
use common\models\c2\search\ProprietorSearch;

use cza\base\components\controllers\backend\ModelController as Controller;
use yii\helpers\Json;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * DefaultController implements the CRUD actions for Proprietor model.
 */
class DefaultController extends Controller
{
    public $modelClass = 'common\models\c2\entity\Proprietor';
    
    /**
     * Lists all Proprietor models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ProprietorSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'model' => $this->retrieveModel(),
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Proprietor model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * create/update a Proprietor model.
     * fit to pajax call
     * @return mixed
     */
    public function actionEdit($id = null) 
    {
        $model = $this->retrieveModel($id);
        
        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                Yii::$app->session->setFlash($model->getMessageName(), [Yii::t('app.c2', 'Saved successful.')]);
            } else {
                Yii::$app->session->setFlash($model->getMessageName(), $model->errors);
            }
        }
        
        return (Yii::$app->request->isAjax) ? $this->renderAjax('edit', [ 'model' => $model,]) : $this->render('edit', [ 'model' => $model,]);
    }

    public function actionEditDevice($id = null)
    {
        $model = UserDevice::find()->where(['proprietor_id' => $id])->one();

        if (empty($model)) {
            $model = new UserDevice();
            $model->loadDefaultValues();
            $model->proprietor_id = $id;
        }

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                Yii::$app->session->setFlash($model->getMessageName(), [Yii::t('app.c2', 'Saved successful.')]);
            } else {
                Yii::$app->session->setFlash($model->getMessageName(), $model->errors);
            }
        }

        return (Yii::$app->request->isAjax) ? $this->renderAjax('edit_device', [ 'model' => $model,]) : $this->render('edit_device', [ 'model' => $model,]);
    }

    /**
     * create/update a Proprietor model.
     * fit to pajax call
     * @return mixed
     */
    public function actionAudit($id = null, $uid = 0)
    {
        $model = $this->retrieveModel($id);

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                return $this->redirect('/fe-user');
            } else {
                Yii::$app->session->setFlash($model->getMessageName(), $model->errors);
            }
        }

        return (Yii::$app->request->isAjax) ? $this->renderAjax('edit', [ 'model' => $model,]) : $this->render('edit', [ 'model' => $model,]);
    }

    public function actionPass($uid = null)
    {
        $user = FeUser::findOne($uid);
        if ($user) {

            $url = \Yii::$app->params['api_url'] . '/api/Building/SetAuditProprietor?supplier_id=' . Yii::$app->params['supplier_id'] . '&proprietor_id=' . $user->proprietor_id;
            $result = Tool::curlGet($url);
            Yii::info($result);

            $result = json_decode($result, true);
            if ($result['Status'] == 0) {
                $user->updateAttributes(['status' => EntityModelStatus::STATUS_ACTIVE]);
                $responseData = ResponseDatum::getSuccessDatum(['message' => Yii::t('cza', 'Operation completed successfully!')], $uid);
            } else {
                $responseData = ResponseDatum::getErrorDatum(['message' => Yii::t('cza', $result['Msg'])], $uid);
            }
            return $this->asJson($responseData);
        }
    }
    
    /**
     * Finds the Proprietor model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Proprietor the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Proprietor::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
